package com.yonyou.pmclouds.organization.process;

import com.yonyou.pmclouds.basecom.bp.processor.MultiProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.SqlUtil;
import com.yonyou.pmclouds.organization.entity.RoleDistributeVO;
import com.yonyou.pmclouds.organization.mapper.RoleDistributeMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;

public class RoleDistributeCheckProcessor implements MultiProcessor<RoleDistributeVO> {

    private RoleDistributeMapper mapper;
    public RoleDistributeCheckProcessor(RoleDistributeMapper mapper){
        this.mapper = mapper;
    }

    @Override
    public void process(RoleDistributeVO[] vos, RoleDistributeVO[] origs) throws BusinessException {
        if(ArrayUtils.isEmpty(vos)){
            return;
        }
        List<String> pkRoles = new ArrayList<>();
        for(RoleDistributeVO vo : vos){
            if(vo != null && StringUtils.isNotEmpty(vo.getPkRole())){
                pkRoles.add(vo.getPkRole());
            }
        }
        if(mapper.countRoleByIds(RuntimeEnvironment.getTenantId(), SqlUtil.getSqlIn(pkRoles.toArray(new String[0]))) > 0){
            throw new BusinessException("提交的角色已经重复！");
        }
    }
}
